<template>
    <div class="app-container">
      <!-- 预约排班列表设置 -->
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
            <el-input v-model="searchObj.hoscode" placeholder="医院编号"></el-input>
        </el-form-item>
        <el-form-item>
            <el-input v-model="searchObj.depcode" placeholder="科室编号"></el-input>
        </el-form-item>
        <el-form-item>
            <el-input v-model="searchObj.docname" placeholder="医生名称"></el-input>
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="findByPage()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
      </el-form>
      <!-- 工具条 -->
      <div>
        <el-button type="danger" size="mini" @click="batchDelete()">批量删除</el-button>
      </div>
      <el-table
        :data="list"
        stripe
        style="width: 100%"
        @selection-change="handleSelection">
        <el-table-column type="selection" width="50" />
        <el-table-column type="index" label="序号" width="50" />
        <el-table-column prop="hoscode" label="医院编号" width="120"  />
        <el-table-column prop="depcode" label="科室编号" width="120" />
        <el-table-column prop="title" label="职称" width="120" />
        <el-table-column prop="docname" label="医生名称" width="120" />
        <el-table-column prop="skill" label="擅长技能" width="120" show-overflow-tooltip="true" />
        <el-table-column prop="workDate" label="排班日期" width="120" />
        <el-table-column label="排班时间" width="120">
            <template slot-scope="scope">
            {{ scope.row.workTime === 0 ? "上午" : "下午" }}
            </template>
        </el-table-column>
        <el-table-column prop="reservedNumber" label="可预约数" width="120" />
        <el-table-column prop="availableNumber" label="剩余预约数" width="120" />
        <el-table-column prop="amount" label="挂号费" width="120" />
        
        <el-table-column label="排班状态" width="120">
            <template slot-scope="scope">
                <span v-show="scope.row.status === 1">可约</span>
                <span v-show="scope.row.status === 0">停约</span>
                <span v-show="scope.row.status === -1">停诊</span>
            </template>
        </el-table-column>
        <el-table-column prop="hosScheduleId" label="排班编号" width="120" />
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button type="danger" size="mini"
            icon="el-icon-delete" @click="deleteById(scope.row.id)">删除</el-button>
            <router-link :to="'/schedule/edit/'+scope.row.id" >
              <el-button type="primary" size="mini" class="margin10"
              icon="el-icon-edit">编辑</el-button>
            </router-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :current-page="current"
        :page-size="limit"
        :total="total"
        style="padding: 30px 0; text-align: center;"
        layout="total, prev, pager, next, jumper"
        @current-change="findByPage"/>
    </div>
  </template>
  
  <style>
    .margin10 {
      margin-left: 10px;
      margin-right: 10px;
    }
  </style>
  
  <script>
  // 引入js文件
  import schedule from '@/api/schedule'
  
  export default {
    // 定义初始值和变量
    data() {
      return {
        current: 1, // 当前页
        limit: 5, // 每页数量
        searchObj: {}, // 条件查询对象
        list: [], // 每页数据集合
        total: 0, // 总记录数
        selectedIds: [] // 选中的数据
      }
    },
    // 在页面渲染之前执行
    created() {
      // 一般调用methods定义的方法，得到数据
      this.findByPage()
    },
    // 定义方法。进行请求接口调用
    methods: {
      // 预约排班列表
      findByPage(page = 1) {
        this.current = page
        schedule.findByPage(this.current, this.limit, this.searchObj)
          .then((response) => {
            // 请求成功后调用
            // console.log(response)
            this.list = response.data.records
            this.total = response.data.total
            console.log(this.list)
          })
          .catch((error) => {
            // 请求失败后调用
            console.log(error)
          })
      },
      //根据id删除数据
      deleteById(id) {
        this.$confirm('确认删除数据？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {//点击确定按钮
          //调用接口删除数据
          schedule.deleteById(id)
          .then((response) => {
            //提示信息
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
            //刷新列表数据
            this.findByPage(1)
          })
          .catch((error) => {
            // 请求失败后调用
            console.log(error)
          })
        }).catch((error) => {
          
        })
      },
      //获取复选框选中的值
      handleSelection(selection) {
        this.selectedIds = []
        for(var i = 0; i < selection.length; i++) {
          this.selectedIds.push(selection[i].id)
        }
      },
      //批量删除
      batchDelete() {
        if (this.selectedIds.length == 0) {
          this.$message({
            message: '请先选择要删除的数据',
            type: 'warning'
          })
          return
        }
        this.$confirm('确认删除数据？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {//点击确定按钮
          //调用接口删除数据
          schedule.batchDelete(this.selectedIds)
          .then((response) => {
            //提示信息
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
            //刷新列表数据
            this.findByPage(1)
          })
          .catch((error) => {
            // 请求失败后调用
            console.log(error)
          })
        }).catch((error) => {
  
        })
      },
      //清空数据
      resetData() {
            this.searchObj = {}
        }
    }
  }
  </script>
  